import type { PQRequest } from '@prequest/types'
import type { ActionFnByParams } from '../interceptorActionHandler'

type options = {
  include?: Array<string>
  exclude?: Array<string>
}

/**
 * 缓存处理器
 * @param {object} options {include, exclude}
 * @return {object}
 */
export const cacheHandler: ActionFnByParams<options> = (options) => {
  return (request: PQRequest) => {
    const handler = () => {
      const service = request.path.replace(/^\/|\/.*$/g, '')
      if (!options?.include) {
        request.useCache = true
      }
      if (options?.include && options.include.includes(service)) {
        request.useCache = true
      }
      if (options?.exclude && options.exclude.includes(service)) {
        request.useCache = false
      }
      return request
    }
    return { checkHandle: true, value: handler }
  }
}
